#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10004, INF = 0x3f3f3f3f;
int V, dfn_idx, scc_cnt, dfn[MAXN], low[MAXN], SccNo[MAXN];
bool in_st[MAXN];
vector<int> Stk, G[MAXN];
void scc(int u) {
  Stk.push_back(u), in_st[u] = true;
  low[u] = dfn[u] = ++dfn_idx;
  for (int v : G[u]) {
    if (dfn[v] == -1) {
      scc(v);
      low[u] = min(low[u], low[v]);
    } else if (in_st[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (dfn[u] != low[u]) return;
  int k;
  do {
    k = Stk.back();
    Stk.pop_back();
    in_st[k] = false;
    SccNo[k] = scc_cnt;
  } while (k != u);
  scc_cnt++;
}
int solve() {
  Stk.clear();
  fill(dfn, dfn + V, -1), fill(low, low + V, INF);
  dfn_idx = 0, scc_cnt = 0;

  for (int v = 0; v < V; ++v)
    if (dfn[v] == -1) scc(v);
  vector<int> out(scc_cnt, 0);
  for (int u = 0; u < V; ++u)
    for (auto v : G[u])
      if (SccNo[u] != SccNo[v]) out[SccNo[u]]++;
  int comp_idx = -1, comp_cnt = 0;
  for (int c = 0; c < scc_cnt; ++c) {
    if (out[c] != 0) continue;
    comp_idx = c;
    if (++comp_cnt > 1) return 0;
  }
  return count(SccNo, SccNo + V, comp_idx);
}
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0, u, v; i < M; ++i) {
    scanf("%d%d", &u, &v);
    G[u - 1].push_back(v - 1);
  }
  V = N;
  printf("%d\n", solve());
  return 0;
}
